import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tin_flutter/app/routes.dart';

import '../../../../../app/res/app_color.dart';
import '../../../../../app/res/intl.dart';
import '../../../../../app/tools/logger.dart';
import '../../../../widget/common_title_widget.dart';
import '../../main_logic.dart';

/// * 作者: Tao
/// * 日期: 2025/11/4
/// * 时间: 16:42
/// * 描述: 品牌制造商模块
class BrandWidget extends StatefulWidget {

  const BrandWidget({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _BrandWidgetState();
}

class _BrandWidgetState extends State<BrandWidget> {
  final state = Get.find<MainLogic>().state;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
      child: Container(
        decoration: BoxDecoration(
          color: AppColor.white,
          borderRadius: BorderRadius.all(Radius.circular(5.0)),
        ),
        child: Padding(
          padding: EdgeInsets.all(12.0),
          child: Column(
            children: [
              // 标题栏
              CommonTitleWidget(
                  imageUrl: 'assets/images/icon_home_brand.png',
                  title: Intl().brandTitle,
                  content: Intl().brandContent,
                  onTap: () {
                    // 跳转品牌推荐页
                    logger('跳转品牌推荐页');
                    Get.toNamed(Routes.brandRecommend);
                  }),
              SizedBox(height: 5.0),
              // 列表
              GridView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  mainAxisSpacing: 5.0,
                  crossAxisSpacing: 5.0,
                  childAspectRatio: 0.9,
                ),
                itemCount: state.brandList.length,
                itemBuilder: (context, index) {
                  return _buildBrandItem(
                    state.brandList[index].id,
                    state.brandList[index].logo,
                    state.brandList[index].name,
                    state.brandList[index].productCount,
                  );
                },
              )
            ],
          ),
        ),
      ),
    );
  }

  /// 品牌制造商item
  /// * [image] logo
  /// * [label] 品牌名
  /// * [productCount] 数量
  Widget _buildBrandItem(int id, String image, String label, int productCount) {
    return InkWell(
        onTap: () {
          // 跳转品牌详情页
          logger('跳转品牌详情页');
          Get.toNamed(Routes.brandDetail, arguments: id);
        },
        child: Container(
          decoration: BoxDecoration(
            color: AppColor.white,
            borderRadius: BorderRadius.all(Radius.circular(5.0)),
            boxShadow: [
              BoxShadow(
                color: AppColor.grey ?? Colors.grey,
                offset: Offset(0.0, 0.5),
                blurRadius: 5.0,
                spreadRadius: 1.0,
              )
            ],
          ),
          child: Padding(
            padding: EdgeInsets.all(12.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Image.network(image, fit: BoxFit.cover),
                Spacer(),
                Text(label, style: TextStyle(fontSize: 13.0)),
                SizedBox(height: 4.0),
                Text('${Intl().brandCount}$productCount',
                    style: TextStyle(fontSize: 10.0, color: AppColor.grey500)),
              ],
            ),
          ),
        ));
  }
}
